package com.mindflow.hbase.tutorials.crud;

import com.mindflow.hbase.tutorials.util.PropertiesUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.HBaseAdmin;

import java.io.IOException;
import java.util.Properties;

/**
 * creater:yuanzhen1
 * time:16:58
 */
public class HBaseTable {

    public static void main(String[] args) throws IOException {
        Properties props = PropertiesUtils.load("hbase.properties");

        Configuration config = HBaseConfiguration.create();
        config.set("hbase.zookeeper.property.clientPort", props.getProperty("hbase.zookeeper.property.clientPort"));
        config.set("hbase.zookeeper.quorum", props.getProperty("hbase.zookeeper.quorum"));

        // Instantiating HbaseAdmin class
        HBaseAdmin admin = new HBaseAdmin(config);

        admin.close();

//        // Instantiating table descriptor class
//        HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf("emp"));
//
//        // Adding column families to table descriptor
//        tableDescriptor.addFamily(new HColumnDescriptor("personal"));
//        tableDescriptor.addFamily(new HColumnDescriptor("professional"));
//
//        // Execute the table through admin
//        admin.createTable(tableDescriptor);
//        System.out.println(" Table created ");
    }

    public void createTable(String tableName,String rowKeyName){

        Configuration conf = HBaseConfiguration.create();
        Properties props = null;
        try {
            props = PropertiesUtils.load("hbase.properties");
            if(props != null){

            }
            conf.set("hbase.zookeeper.property.clientPort", props.getProperty("hbase.zookeeper.property.clientPort"));
            conf.set("hbase.zookeeper.quorum", props.getProperty("hbase.zookeeper.quorum"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            HBaseAdmin admin = new HBaseAdmin(conf);
            //creating table descriptors
            HTableDescriptor table = new HTableDescriptor(toBytes("hbaseTable"));
            //creating column family descriptor
            HColumnDescriptor family = new HColumnDescriptor(toBytes("columnTest"));
            //adding coloumn family to HTable
            table.addFamily(family);

            admin.createTable(table);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    private static byte[] toBytes(String content){
        if(content != null){
            return content.getBytes();
        }
        return null;
    }
}
